/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TablaInventario.java
 *
 * Created on 04-ago-2011, 11:07:10
 */
package proyectoempresa.igu;

import Utildao.Utildao;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import org.edisoncor.gui.button.ButtonColoredAction;
import proyectoempresa.DAO.InventarioDao;
import proyectoempresa.modelo.Inventario;
import proyectoempresa.modeloTabla.ModeloTablaInventario;

/**
 *
 * @author Diego
 */
public class TablaInventario extends javax.swing.JDialog {

    /** Creates new form TablaInventario */
    public TablaInventario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setTitle("Ferretria Fhersa");
        setLocationRelativeTo(null);
    }

    public JTable getTablaInventario() {
        return tablaInventario;
    }

    public void setTablaInventario(JTable tablaInventario) {
        this.tablaInventario = tablaInventario;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelImage1 = new org.edisoncor.gui.panel.PanelImage();
        jScrollPane1 = new javax.swing.JScrollPane();
        InventarioDao indao= new InventarioDao(null);

        ModeloTablaInventario modelo= new ModeloTablaInventario(indao.buscarTodos());
        tablaInventario = new javax.swing.JTable();
        buttonColoredAction1 = new org.edisoncor.gui.button.ButtonColoredAction();
        buttonColoredAction2 = new org.edisoncor.gui.button.ButtonColoredAction();
        buttonColoredAction3 = new org.edisoncor.gui.button.ButtonColoredAction();
        buttonColoredAction4 = new org.edisoncor.gui.button.ButtonColoredAction();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Imagenes/38-w500-h375.jpg"))); // NOI18N
        panelImage1.setName("panelImage1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaInventario.setModel(modelo);
        tablaInventario.setName("tablaInventario"); // NOI18N
        jScrollPane1.setViewportView(tablaInventario);

        buttonColoredAction1.setText("GUARDAR");
        buttonColoredAction1.setName("buttonColoredAction1"); // NOI18N
        buttonColoredAction1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction1ActionPerformed(evt);
            }
        });

        buttonColoredAction2.setText("EDITAR");
        buttonColoredAction2.setName("buttonColoredAction2"); // NOI18N
        buttonColoredAction2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction2ActionPerformed(evt);
            }
        });

        buttonColoredAction3.setText("ELIMINAR");
        buttonColoredAction3.setName("buttonColoredAction3"); // NOI18N
        buttonColoredAction3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction3ActionPerformed(evt);
            }
        });

        buttonColoredAction4.setText("SALIR");
        buttonColoredAction4.setName("buttonColoredAction4"); // NOI18N
        buttonColoredAction4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction4ActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Bradley Hand ITC", 3, 48)); // NOI18N
        jLabel6.setText("INVENTARIO");
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout panelImage1Layout = new javax.swing.GroupLayout(panelImage1);
        panelImage1.setLayout(panelImage1Layout);
        panelImage1Layout.setHorizontalGroup(
            panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelImage1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 733, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonColoredAction1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48))
            .addGroup(panelImage1Layout.createSequentialGroup()
                .addGap(282, 282, 282)
                .addComponent(jLabel6)
                .addContainerGap(330, Short.MAX_VALUE))
        );
        panelImage1Layout.setVerticalGroup(
            panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelImage1Layout.createSequentialGroup()
                .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelImage1Layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(buttonColoredAction1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buttonColoredAction2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonColoredAction3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonColoredAction4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelImage1Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel6)
                        .addGap(37, 37, 37)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(57, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelImage1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelImage1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public ButtonColoredAction getButtonColoredAction4() {
        return buttonColoredAction4;
    }

    public void setButtonColoredAction4(ButtonColoredAction buttonColoredAction4) {
        this.buttonColoredAction4 = buttonColoredAction4;
    }

    private void buttonColoredAction1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction1ActionPerformed
        // TODO add your handling code here:
        ModeloTablaInventario modelo = (ModeloTablaInventario) tablaInventario.getModel();
        new InventarioIgu(new JFrame(), true, modelo,new Inventario(), 1, tablaInventario).setVisible(true);
        modelo.fireTableDataChanged();
         
    }//GEN-LAST:event_buttonColoredAction1ActionPerformed

    private void buttonColoredAction2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction2ActionPerformed
        // TODO add your handling code here:
         if (tablaInventario.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(null,"Debe seleccionar el Registro a Editar ");
         }else{
        ModeloTablaInventario modelo = (ModeloTablaInventario) tablaInventario.getModel();
        new InventarioIgu(new JFrame(), true, modelo, modelo.getInventarioSeleccionado(), 2,tablaInventario).setVisible(true);
        modelo.fireTableDataChanged();
        }
    }//GEN-LAST:event_buttonColoredAction2ActionPerformed
   InventarioDao ivd=null;
    private void buttonColoredAction3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction3ActionPerformed
         if (tablaInventario.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(null, "Debe Seleccionar el o los Registros a Eliminar............");
         }else{
        ModeloTablaInventario modelo = (ModeloTablaInventario) tablaInventario.getModel();
      int[] selected =tablaInventario.getSelectedRows();
        System.out.println(selected[0]);
       int op = JOptionPane.showConfirmDialog(this, "¿REALMENTE DESEA ELIMINAR.....?");
                if (op == 0) {
                List<Inventario> toRemove = new ArrayList<Inventario>(selected.length);
      try {
                for (int idx=0; idx<selected.length; idx++) {
                Inventario iv = modelo.getFilas().get(tablaInventario.convertRowIndexToModel(selected[idx]));
                 ivd= new InventarioDao(iv);
                System.out.println(iv);
                toRemove.add(iv);
                ivd.remover();
          }
          modelo.getFilas().removeAll(toRemove);
         modelo.fireTableDataChanged();
          JOptionPane.showMessageDialog(null, "Se  Elimino ");
                     } catch (Exception ex) {
                Logger.getLogger(TablaInventario.class.getName()).log(Level.SEVERE, null, ex);
            }
                }
        }
    }//GEN-LAST:event_buttonColoredAction3ActionPerformed

    private void buttonColoredAction4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction4ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_buttonColoredAction4ActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                TablaInventario dialog = new TablaInventario(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction1;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction2;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction3;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.edisoncor.gui.panel.PanelImage panelImage1;
    private javax.swing.JTable tablaInventario;
    // End of variables declaration//GEN-END:variables
}
